<template>
  <div class="emp-container">
    <page-tools>
      <!-- <div slot='right'> -->
      <!-- <template #right> -->
      <template v-slot:left>
        <div class="left">
          <i class="el-icon-info" />
          <span>总记录数：{{ $store.state.employees.total }}条</span>
        </div>
      </template>
      <template v-slot:right>
        <el-button
          v-arror="'download'"
          type="primary"
          size="small"
          @click="$router.push('/import')"
        >导入excel</el-button>
        <el-button
          type="info"
          size="small"
          @click="handleDownload"
        >导出excel</el-button>
        <el-button
          type="success"
          size="small"
          @click="addEmp"
        >添加员工</el-button>
      </template>
    </page-tools>

    <!-- 表格 -->
    <el-table
      :default-sort="{ prop: 'workNumber', order: 'descending' }"
      :data="$store.state.employees.tableData"
      border
      style="width: 100%"
    >
      <el-table-column type="index" label="序号" width="150" />
      <el-table-column sortable prop="username" label="姓名" width="120" />
      <el-table-column sortable prop="mobile" label="手机号" width="120" />
      <el-table-column sortable prop="workNumber" label="工号" width="120" />
      <el-table-column sortable label="聘用形式" width="130">
        <template slot-scope="scope">
          {{ formatEmployment(scope.row.formOfEmployment) }}
        </template>
      </el-table-column>
      <el-table-column
        sortable
        prop="departmentName"
        label="部门"
        width="120"
      />
      <el-table-column sortable label="入职时间" width="120">
        <template slot-scope="scope">
          {{ scope.row.timeOfEntry | formatDate }}
        </template>
      </el-table-column>
      <el-table-column
        sortable
        prop="enableState"
        label="是否在职"
        width="120"
      />
      <el-table-column
        sortable
        prop="inServiceStatus"
        label="状态"
        width="120"
      />
      <el-table-column fixed="right" label="操作" width="250">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="$router.push('/employees/detail?id=' + scope.row.id)"
          >查看</el-button>
          <el-button type="text" size="small">转正</el-button>
          <el-button type="text" size="small">{{
            $t('route.employeesAdjust')
          }}</el-button>
          <el-button type="text" size="small">{{
            $t('route.employeesLeave')
          }}</el-button>
          <el-button
            type="text"
            size="small"
            @click="hEditRole(scope.row)"
          >角色</el-button>
          <el-button
            type="text"
            size="small"
            @click="delMan(scope.row.id)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 表格 -->

    <!-- 分页 -->
    <el-pagination
      :current-page.sync="paramsObj.page"
      :page-size="paramsObj.size"
      :page-sizes="[1, 3, 5, 7, 10]"
      layout="slot, prev, pager, next"
      :total="$store.state.employees.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    >共{{ $store.state.employees.total }}条
    </el-pagination>
    <!-- 对话框 -->
    <el-dialog
      title="提示"
      :visible.sync="$store.state.employees.dialogVisible"
      width="45%"
    >
      <!-- 表单布局==抽里程一个公共组件 -->
      <addDia :params-obj.sync="paramsObj" />
    </el-dialog>
    <el-dialog
      title="分配角色"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :visible.sync="showDialogRole"
    >
      <assign-role
        v-if="showDialogRole"
        ref="qqww"
        :employee-id="curEmployeId"
        @close="showDialogRole = false"
      />
    </el-dialog>
  </div>
</template>

<script>
import EmployeeEnum from '@/api/constant/employees'
import { delEmployee, getUser } from '@/api/employees'
import addDia from './components/addDia.vue'
// 引入组件
import AssignRole from './assignRole'
export default {
  name: 'Employess',
  components: {
    addDia,
    AssignRole
  },
  data() {
    return {
      paramsObj: {
        page: 1,
        size: 5
      },
      showDialogRole: false,
      curEmployeId: '' // 当前的员工编号
    }
  },
  computed: {},
  created() {
    this.$store.dispatch('employees/getUser', this.paramsObj)
  },
  methods: {
    /* 分页 */
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      this.paramsObj.size = val
      this.$store.dispatch('employees/getUser', this.paramsObj)
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      this.paramsObj.page = val
      this.$store.dispatch('employees/getUser', this.paramsObj)
    },
    handleClick(row) {
      // console.log(row)
    },
    formatEmployment(num) {
      // 从hireType中 通过id找到对应的元素的value
      // console.log(EmployeeEnum, 88)
      const { hireType } = EmployeeEnum
      const item = hireType.find((it) => it.id === num)
      if (item) {
        return item.value
      } else {
        return '不能确定'
      }
      // return  ? '正式' : '非正式'
    },
    async delMan(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          await delEmployee(id)
          // 确认按钮
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          //  成功后 刷新页面
          // 判断当前页是否是最后一个,是就跳转到上一页
          if (
            this.$store.state.employees.total % this.paramsObj.size === 1 &&
            Math.floor(
              this.$store.state.employees.total / this.paramsObj.size
            ) +
              1 ===
              this.paramsObj.page
          ) {
            this.paramsObj.page = this.paramsObj.page - 1
            this.$store.dispatch('employees/getUser', this.paramsObj)
          }
          this.$store.dispatch('employees/getUser', this.paramsObj)
        })
        .catch(() => {
          // 点击取消按钮
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    addEmp() {
      this.$store.commit('employees/change', true)
    },
    async handleDownload() {
      const res = await getUser({
        page: 1,
        size: this.$store.state.employees.total
      })

      import('@/vendor/Export2Excel').then((excel) => {
        const list = res.data.rows
        const { header, data } = this.formatData(list)
        excel.export_json_to_excel({
          header,
          data,
          filename: 'excel-list', // 文件名称
          autoWidth: true, // 宽度是否自适应
          bookType: 'xlsx' // 生成的文件类型
        })
      })
    },
    formatData(list) {
      const map = {
        id: '编号',
        password: '密码',
        mobile: '手机号',
        username: '姓名',
        timeOfEntry: '入职日期',
        formOfEmployment: '聘用形式',
        correctionTime: '转正日期',
        workNumber: '工号',
        departmentName: '部门',
        staffPhoto: '头像地址'
      }
      let header = []
      let data = []
      header = Object.values(map)
      data = list.map((obj) => {
        const dd = []
        const xx = Object.keys(map)
        xx.forEach((item) => {
          dd.push(obj[item])
        })
        return dd
      })
      return { header, data }
    },
    hEditRole({ id }) {
      // console.log('当前要分配角色id是', id)
      // this.$nextTick(() => {
      //   this.$refs.qqww.loadRoles()
      // })
      this.curEmployeId = id
      this.showDialogRole = true
    }
  }
}
</script>

<style lang="scss" scoped>
.left {
  padding: 10px;
  border: 1px solid #c2e8ff;
  border-radius: 5px;
  background-color: #c2e8ff;
  .el-icon-info {
    color: #4199ff;
    margin-right: 5px;
  }
}

.el-pagination {
  text-align: right;
}
.emp-container {
  ::v-deep .el-dialog__header {
    background-color: #409eff;
    height: 50px;
  }
}
</style>
